export const mp3PlayMixin = {
	data() {
		return {
			isPlaying: false,
			isPlayEnd: false,
			duration: 100,
			timeStr: '00:00',
		}

	},
	onload() {
		this._isChanging = false;
		this._audioContext = null;
		// this.createAudio();
	},
	methods: {
		// 音频播放
		createAudio() {
			// uni.showLoading({
			// 	title: '音频加载中'
			// })
			let src = this.baseUrl + this.audioUrl
			// var innerAudioContext = this._audioContext = uni.createInnerAudioContext();
			var innerAudioContext = this._audioContext = uni.getBackgroundAudioManager()
			innerAudioContext.autoplay = false;
			innerAudioContext.playbackRate = this.beisu;
			innerAudioContext.src = src;
			innerAudioContext.startTime = this.detailInfo.startTime
			innerAudioContext.coverImgUrl = this.detailInfo.cover_image;
			innerAudioContext.title = this.detailInfo.title
			// innerAudioContext.stop()
			innerAudioContext.onPlay(() => {
				this.isPlaying = true
				console.log('开始播放');
			});
			innerAudioContext.onStop(() => {
				this.isPlaying = false
			})
			innerAudioContext.onCanplay(() => {
				let timeid = setInterval(() => {
					if (innerAudioContext.duration) {
						clearInterval(timeid)
						this.duration = innerAudioContext.duration || 0;
						console.log('282828',this.duration)
						this.timeStr = this.formatSecond(this.duration);
						uni.hideLoading()
					}
				}, 500)
			});
			innerAudioContext.onTimeUpdate((e) => {
				if (this._isChanging === true) {
					return;
				}
				this.currentTime = innerAudioContext.currentTime || 0;
				this.playTime = this.currentTime
				this.duration = innerAudioContext.duration || 0;
				this.currentTimeStr = this.formatTime(this.currentTime);
				// 进度条最大值
				this.timeStr = this.formatSecond(this.duration);
			});
			innerAudioContext.onEnded(() => {
				this.currentTime = 0;
				this.currentTimeStr = this.formatTime(this.currentTime);
				this.isPlaying = false;
				this.isPlayEnd = true;
				this.loadMore()
			});
			innerAudioContext.onError((res) => {
				this.isPlaying = false;
				console.log('12120',res);
			});
			return innerAudioContext;
		},
		onchanging() {
			this._isChanging = true;
		},
		onchange(e) {
			console.log('24242424',e.detail.value);
			console.log('252525',typeof e.detail.value);
			this._audioContext.seek(e.detail.value);
			this._isChanging = false;
			const currTimeStr = this.formatTime(e.detail.value)
			this.currentTimeStr = currTimeStr
		},
		handleRate() {
			if(this.beisu == 2.0){
				this.beisu = 1.0;
			}else{
				this.beisu = 2.0
			}
			
			this._audioContext.stop();
			this._audioContext.playbackRate = this.beisu;
			this._audioContext.play();
			this.createAudio()
		},
		async play() {
			// await this.createAudio()
			if (this.isPlaying) {
				this.pause();
				return;
			}
			this._audioContext.play();
			this.isPlaying = true;
			this.isPlayEnd = false;
		},
		pause() {
			this._audioContext.pause();
			this.isPlaying = false;
			console.log('mp3暂停'),
			this.returnPlaytime()
		},
		stop() {
			this._audioContext.stop();
			this.isPlaying = false;
			this.returnPlaytime()
		},
		formatTime(num) {
			//格式化时间格式
			num = num.toFixed(0);
			let second = num % 60;
			if (second < 10) second = '0' + second;
			let min = Math.floor(num / 60);
			if (min < 10) min = '0' + min;
			return min + ":" + second;
		},
		/**
		 * 格式化时间 
		 * @param {String} date 原始时间格式
		 * 格式后的时间：hh:mm:ss
		 **/
		formatSecond(seconds) {
			console.log('27',seconds)
			// var h = Math.floor(seconds / 3600) < 10 ? '0' + Math.floor(seconds / 3600) : Math.floor(seconds / 3600);
			var m = (parseInt(seconds / 60) > 99) ? parseInt(seconds / 60).toString().padStart(3, '0') : parseInt(
				seconds / 60).toString().padStart(2, '0')
			var s = parseInt(seconds % 60).toString().padStart(2, '0');
			return m + ":" + s;
		}
	}
}
